package com.changgou.contoller;

import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSUtil;
import entity.Result;
import entity.StatusCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @auther LiZiBa
 * @date 2020/7/26 16:22
 * @description:
 **/

@RestController
@RequestMapping(value = "/upload")
@CrossOrigin
public class FileUploadController {

    @PostMapping("")
    public Result upload(@RequestParam("file")MultipartFile multipartFile) throws  Exception{

        FastDFSFile fastDFSFile = new FastDFSFile(
                multipartFile.getOriginalFilename(), // 文件名 LiZiBa.png
                multipartFile.getBytes(), // 文件字节内容
                StringUtils.getFilenameExtension(multipartFile.getOriginalFilename()) // 文件后缀名 png

        );

        String[] uploads = FastDFSUtil.upload(fastDFSFile);
        String url = "http://192.168.211.132:8080/" + uploads[0] + "/" + uploads[1];

        return new Result(true, StatusCode.OK, "文件上传成功！", url);

    }


}
